//
//  XSImageRatioView.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/9/25.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSImageRatioView.h"


@implementation XSImageRatio
{
    CGFloat _longSide;
    CGFloat _shortSide;
}

- (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height{
    self = [super init];
    if (self) {
        _longSide = MAX(width, height);
        _shortSide = MIN(width, height);
    }
    return self;
}

- (CGFloat)ratio{
    if (_shortSide == 0 || _longSide == 0) {
        return 0;
    }
    
    if (self.isLandscape) {
        return _shortSide/_longSide;
    }
    return _longSide/_shortSide;
}

- (NSString *)titleFormat{
    if (_shortSide == 0 || _longSide == 0) {
        return @"Custom";
    }
    
    if (self.isLandscape) {
        return [NSString stringWithFormat:@"%g : %g",_longSide,_shortSide];
    }
    return [NSString stringWithFormat:@"%g : %g",_shortSide,_longSide];
}



@end


@interface XSImageRatioView ()
{
    UIImageView *_iconView;
    UILabel *_titleLabel;
}

@end

@implementation XSImageRatioView

- (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)image target:(id)target action:(SEL)action
{
    self = [super initWithFrame:frame];
    if(self){
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
        [self addGestureRecognizer:gesture];
        
        CGFloat W = frame.size.width;
        _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, W-20, W-20)];
        _iconView.clipsToBounds = YES;
        _iconView.image = image;
        _iconView.layer.cornerRadius = 5;
        _iconView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_iconView];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_iconView.frame) + 5, W, 15)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:10];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    return self;
}

- (void)refreshViews{
    _titleLabel.text = self.ratio.titleFormat;
    CGPoint center = _iconView.center;
    CGFloat w , h;
    if (_ratio.ratio != 0) {
        if (_ratio.isLandscape) {
            w = 50;
            h = 50 *_ratio.ratio;
        }else{
            w = 50 / _ratio.ratio;
            h = 50;
        }
    }else{
        CGFloat maxW = MAX(_iconView.image.size.width, _iconView.image.size.height);
        w = 50* _iconView.image.size.width/maxW;
        h = 50* _iconView.image.size.height/maxW;
    }
    
    _iconView.frame = CGRectMake(center.x - w/2, center.y - h/2, w, h);
}

- (void)changeOrientation{
    self.ratio.isLandscape = !self.ratio.isLandscape;
    [UIView animateWithDuration:0.2 animations:^{
        [self refreshViews];
    }];
}

@end


//
@interface XSImageRatioToolBarView ()

@property(nonatomic,strong)UIImage * iconImage;
@property (nonatomic, strong)UIButton *rotateRatioBtn;
@property(nonatomic,strong)NSMutableArray *ratioViews;

@end

@implementation XSImageRatioToolBarView

- (instancetype)initWithFrame:(CGRect)frame iconImage:(UIImage *)iconImage
{
    self = [super initWithFrame:frame];
    if (self) {
        self.iconImage = iconImage;
        _ratioViews = [NSMutableArray array];
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI{
    NSArray *array = @[@{@"value1":@"0",@"value2":@"0"},
                       @{@"value1":@"1",@"value2":@"1"},
                       @{@"value1":@"4",@"value2":@"3"},
                       @{@"value1":@"3",@"value2":@"2"},
                       @{@"value1":@"16",@"value2":@"9"}];
    CGFloat W = 70;
    CGFloat H = 80;
    CGFloat x = 0;
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:scrollView];
    
    for (NSDictionary *info in array) {
        CGFloat val1 = [info[@"value1"] floatValue];
        CGFloat val2 = [info[@"value2"] floatValue];
        XSImageRatio *ratio = [[XSImageRatio alloc]initWithWidth:val1 height:val2];
        ratio.isLandscape = val1 > val2;
        
        XSImageRatioView *ratioView = [[XSImageRatioView alloc]initWithFrame:CGRectMake(x , 0 , W, H) image:self.iconImage target:self action:@selector(tapRadio:)];
        ratioView.ratio = ratio;
        [ratioView refreshViews];
        [scrollView addSubview:ratioView];
        [self.ratioViews addObject:ratioView];
        
        if (!self.selectedRatioView) {
            self.selectedRatioView = ratioView;
        }
        
        x += W;
    }
    
    //旋转按钮
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x , 0, W, H)];
    view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:.8];
//    view.hidden = YES;
    [scrollView addSubview:view];
    _rotateRatioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rotateRatioBtn.frame = CGRectMake(15, 20, 40, 40);
    _rotateRatioBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_rotateRatioBtn setBackgroundImage:[UIImage imageNamed:@"zl_btn_rotate"] forState:UIControlStateNormal];
    [_rotateRatioBtn addTarget:self action:@selector(rotateRadioBtn_click) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_rotateRatioBtn];
    
    
    scrollView.contentSize = CGSizeMake(x + W, H);
    
}

- (void)rotateRadioBtn_click{
    for (XSImageRatioView *ratioV in self.ratioViews) {
        [ratioV changeOrientation];
    }
    
    if (self.selectedRatioView.ratio.ratio != 0 && self.selectedRatioView.ratio.ratio != 1) {
        if (self.currentSelectRatioBlock){
            self.currentSelectRatioBlock(self.selectRatio);
        }
    }
    
}

- (void)tapRadio:(UITapGestureRecognizer *)sender{
    self.selectedRatioView = (XSImageRatioView *)sender.view;
    
}
- (void)setSelectedRatioView:(XSImageRatioView *)selectedRatioView{
    if (selectedRatioView != _selectedRatioView) {
        _selectedRatioView.backgroundColor = [UIColor clearColor];
        _selectedRatioView = selectedRatioView;
        _selectedRatioView.backgroundColor = [UIColor colorWithWhite:1 alpha:.15];
        
        if(selectedRatioView.ratio.ratio==0){
            self.selectRatio = nil;
        } else {
            self.selectRatio = selectedRatioView.ratio;
        }
        
        if (self.currentSelectRatioBlock){
            self.currentSelectRatioBlock(self.selectRatio);
        }
    }
    
}


@end
